package com.songcha.library_base.compose

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.songcha.library_base.base.BaseFragment
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_base.mvvm.base.IMvvm
import kotlinx.coroutines.launch
import java.lang.reflect.ParameterizedType


abstract class BaseComposeMvvmFragment<VM: BaseViewModel<out BaseRepository>> : BaseComposeFragment(),
    IMvvm {
    lateinit var viewModel:VM

    //private var mRootView:View?=null

    fun isViewModelInitialized()=::viewModel.isInitialized

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        viewModel.state.observe(viewLifecycleOwner){
            when(it){
                BaseViewModel.LoadState.LOADING->onLoading()
                BaseViewModel.LoadState.LOAD_SUCCESS->onLoadSuccess()
                BaseViewModel.LoadState.LOAD_ERROR->onLoadError(viewModel.error!!)
                BaseViewModel.LoadState.LOAD_NO_NETWORK->onLoadNoNetwork()
                BaseViewModel.LoadState.LOAD_CONNECT_ERROR->onLoadConnectError()
                BaseViewModel.LoadState.LOAD_NO_DATA->onLoadNoData()
                BaseViewModel.LoadState.LOAD_TIMEOUT->onLoadTimeOut()
            }
        }

        initView(savedInstanceState)
        viewModel.isInit=true

        initObserver()

        initFlowCollect()

        if(isInitViewGetData())
            onGetData()
    }

    //if getdata on resume(),then no need to getdata on initview()
    protected open fun isInitViewGetData():Boolean{
        return true
    }


    override fun initOnCreateView():View?{
        //get genericity type
        val cls=(this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        viewModel=ViewModelProvider(this).get(cls)

        //var rootView=getRootView()?:LayoutInflater.from(requireContext()).inflate(getLayoutId(),null)
        //mRootView=super.initOnCreateView()
        val rootView = super.initOnCreateView()

        if(rootView!=null){
            onVMDBInitialized()
        }

        //rootView=super.initLoad(rootView)

        return rootView
    }

    //viewmodel and databing init completely
    protected open fun onVMDBInitialized(){

    }

    protected open fun onGetData(){}

    protected open fun initObserver(){}

    protected open fun initFlowCollect(){}

    protected open fun collectOn(block:suspend ()->Unit){
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                block()
            }
        }
    }

    override fun onRetry() {
        onGetData()
    }



}